#include <gflags/gflags.h>
#include <thread>
#include <gtest/gtest.h>

#include "file.pb.h"
#include "aip-cpp-sdk-4.16.7/speech.h"
#include "etcd.hpp"
#include "channel.hpp"
#include "logger.hpp"
#include "utils.hpp"

DEFINE_bool(run_mode,false,"程序的运行模式，false： 调试 true： 发布");
DEFINE_string(log_file,"","发布模式下，向文件中输出，如果是调试模式下，模式是stdout");
DEFINE_int32(log_level,0,"发布模式下，用于指定的日志输出等级");

DEFINE_string(etcd_host,"http://127.0.0.1:2379","服务注册中心地址");
DEFINE_string(base_service,"/service","服务监控根目录");
DEFINE_string(file_service,"/service/file_service","服务监控根目录");

ServiceChannel::ChannelPtr channel;
std::string single_file_id;

TEST(put_test,single_test) {
    //读取当前文件,到body中
    std::string body;
    ASSERT_TRUE(readFile("./Makefile",body));
    //实例化rpc对象。发起rpc调用
    lch_im::FileService_Stub  stub(channel.get());

    lch_im::PutSingleFileReq req;
    req.set_request_id("1111");
    req.mutable_file_data()->set_file_name("./Makefile");
    req.mutable_file_data()->set_file_size(body.size());
    req.mutable_file_data()->set_file_content(body);

    brpc::Controller *cntl = new brpc::Controller();
    lch_im::PutSingleFileRsp *rsp = new lch_im::PutSingleFileRsp();
    //发起rpc调用，进行文件上传
    stub.PutSingleFile(cntl, &req, rsp, nullptr);
    ASSERT_FALSE(cntl->Failed());

    //检测是否成功
    ASSERT_TRUE(rsp->success());
    ASSERT_EQ(rsp->file_info().file_size(),body.size());
    ASSERT_EQ(rsp->file_info().file_name(),"./Makefile");
    single_file_id = rsp->file_info().file_id();
    LOG_DEBUG("文件ID： {}",rsp->file_info().file_id());
}

TEST(get_test,single_test) {
    lch_im::FileService_Stub stub(channel.get());

    lch_im::GetSingleFileReq req;
    lch_im::GetSingleFileRsp* rsp;
    req.set_request_id("2222");
    req.set_file_id(single_file_id);

    brpc::Controller* cntl = new brpc::Controller();
    rsp = new lch_im::GetSingleFileRsp();
    stub.GetSingleFile(cntl,&req,rsp,nullptr);
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success());
    ASSERT_EQ(single_file_id,rsp->file_data().file_id());
    //将文件数据存储到文件中
    writeFile("./make_file_download",rsp->file_data().file_content());
}

int main(int argc,char* argv[])
{
    testing::InitGoogleTest(&argc,argv);
    google::ParseCommandLineFlags(&argc,&argv,true);

    init_logger(FLAGS_run_mode,FLAGS_log_file,FLAGS_log_level);

        //1. 先构造Rpc信道管理对象
    auto sm = std::make_shared<ServiceManager>();
    sm->declared(FLAGS_file_service);
    auto put_cb = std::bind(&ServiceManager::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&ServiceManager::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    //2. 构造服务发现对象
    Discovery::ptr dclient = std::make_shared<Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    
    //3. 通过Rpc信道管理对象，获取提供Echo服务的信道
    channel = sm->choose(FLAGS_file_service);
    if (!channel) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        return -1;
    }

    return RUN_ALL_TESTS();
}